package com.libre.config.mybatisplus;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author 庞留杰
 * @version V1.0
 * @description 处理新增和更新的基础数据填充，配合BaseEntity和MyBatisPlusConfig使用
 * @date 2020/7/22 20:38
 **/
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

//    @Autowired
//    SecurityUtil securityUtil;

    /**
     * 功能描述: 新增数据执行
     *
     * @param metaObject
     * @throws
     * @author: 庞留杰
     * @return:
     * @date: 2020/7/16 19:20
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        System.out.println(">>>>>>>>>>>>>insertFill>>>>>>>>>>>>>>>");
//        User user = securityUtil.getLoginUser();
//        this.setFieldValByName("created_by", user.getId(), metaObject);
//        this.setFieldValByName("updated_by", user.getId(), metaObject);
//        this.setFieldValByName("created_time", new Date(), metaObject);
//        this.setFieldValByName("updated_time", new Date(), metaObject);
        this.strictInsertFill(metaObject, "created_time", () -> LocalDateTime.now(), LocalDateTime.class);
        this.strictInsertFill(metaObject, "updated_time", () -> LocalDateTime.now(), LocalDateTime.class);

    }

    /**
     * 功能描述: 更新数据执行
     *
     * @param metaObject
     * @throws
     * @author: 庞留杰
     * @return:
     * @date: 2020/7/16 19:20
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        System.out.println(">>>>>>>>>>>>>updateFill>>>>>>>>>>>>>>>");
//        User user = securityUtil.getLoginUser();
//        this.setFieldValByName("updated_by", user.getId(), metaObject);
        this.strictUpdateFill(metaObject, "updated_time", () -> LocalDateTime.now(), LocalDateTime.class);
    }

}
